package com.demo;

import com.entity.Comment;
import com.entity.Goods;
import com.service.GoodsService;
import com.service.CommentService;
import com.service.StudentService;
import com.entity.Student;
import com.entity.Report; // 引入 Report
import com.service.ReportService; // 引入 ReportService
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.List;
import java.util.Optional; // 引入 Optional 用于查找

/**
 * 演示评论、举报及管理员操作功能的运行类
 */


public class DemoRunner {
    public static void main(String[] args) {
        // 加载基于 XML 的 Spring 上下文
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取服务 Bean
        StudentService studentService = context.getBean(StudentService.class);
        GoodsService goodsService = context.getBean(GoodsService.class);
        CommentService commentService = context.getBean(CommentService.class);
        ReportService reportService = context.getBean(ReportService.class); // 获取 ReportService

        System.out.println("=== 1. 添加新评价（评论） ===");
        Optional<Student> student1Opt = Optional.ofNullable(studentService.findById(1L));
        Optional<Goods> goods1Opt = Optional.ofNullable(goodsService.findById(1L));
        Comment addedComment = null; // 用于后续删除
        if (student1Opt.isPresent() && goods1Opt.isPresent()) {
            Comment newEval = new Comment();
            newEval.setStudent(student1Opt.get());
            newEval.setGoods(goods1Opt.get());
            newEval.setContent("还能再降点吗");
            addedComment = commentService.save(newEval);
            if (addedComment != null) {
                System.out.println("成功添加评价: " + addedComment);
            } else {
                System.out.println("添加评价失败！");
            }
        } else {
            System.out.println("无法找到学生ID=1 或 商品ID=1，未能添加评价。");
        }
        System.out.println();

        System.out.println("=== 2. 管理员：查询所有评论 ===");
        List<Comment> commentsBeforeDelete = commentService.findAll();
        if (commentsBeforeDelete != null && !commentsBeforeDelete.isEmpty()) {
            commentsBeforeDelete.forEach(c -> System.out.println("  " + c));
        } else {
            System.out.println("  当前无评论。");
        }
        System.out.println();

        System.out.println("=== 3. 管理员：删除评论 ===");
        if (addedComment != null && addedComment.getId() != null) {
            long commentIdToDelete = addedComment.getId();
            System.out.println("尝试删除评论 ID=" + commentIdToDelete + " ...");
            boolean delSuccess = commentService.deleteById(commentIdToDelete);
            System.out.println("删除评论 ID=" + commentIdToDelete + " : " + (delSuccess ? "成功" : "失败"));
        } else {
            System.out.println("无评论可删除。");
        }
        System.out.println();

        System.out.println("=== 4. 管理员：查询所有评论（删除后） ===");
        List<Comment> commentsAfterDelete = commentService.findAll();
        if (commentsAfterDelete != null && !commentsAfterDelete.isEmpty()) {
            commentsAfterDelete.forEach(c -> System.out.println("  " + c));
        } else {
            System.out.println("  删除后无评论。");
        }
        System.out.println();

        System.out.println("=== 5. 添加新举报 ===");
        Optional<Student> students = Optional.ofNullable(studentService.findById(1L));
        Optional<Goods> goods = Optional.ofNullable(goodsService.findById(1L));
        Report addedReport = null; // 用于后续删除

        if (students.isPresent() && goods.isPresent()) {
            Report newReport = new Report();
            newReport.setStudent(students.get());
            newReport.setGoods(goods.get());
            newReport.setContent("商品描述与实际不符，图片与实物差距较大");
            try {
                addedReport = reportService.save(newReport);
                if (addedReport != null) {
                    System.out.println("新举报添加成功：" + addedReport);
                } else {
                    System.out.println("添加新举报失败");
                }
            } catch (Exception e) {
                System.out.println("添加新举报失败：" + e.getMessage());
            }
        } else {
            System.out.println("无法找到学生ID=1 或 商品ID=1，未能添加举报。");
        }
        System.out.println();

        System.out.println("=== 6. 管理员：查询所有举报 ===");
        List<Report> reportsBeforeDelete = reportService.findAll();
        if (reportsBeforeDelete != null && !reportsBeforeDelete.isEmpty()) {
            reportsBeforeDelete.forEach(r -> System.out.println("  举报：" + r));
        } else {
            System.out.println("  当前无举报。");
        }
        System.out.println();

        System.out.println("=== 7. 管理员：删除举报 ===");
        if (addedReport != null && addedReport.getId() != null) {
            long reportIdToDelete = addedReport.getId();
            System.out.println("尝试删除举报 ID=" + reportIdToDelete + " ...");
            boolean delSuccess = reportService.deleteById(reportIdToDelete);
            System.out.println("删除举报 ID=" + reportIdToDelete + " : " + (delSuccess ? "成功" : "失败"));
        } else {
            System.out.println("无举报可删除。");
        }
        System.out.println();

        System.out.println("=== 8. 管理员：查询所有举报（删除后） ===");
        List<Report> reportsAfterDelete = reportService.findAll();
        if (reportsAfterDelete != null && !reportsAfterDelete.isEmpty()) {
            reportsAfterDelete.forEach(r -> System.out.println("  举报：" + r));
        } else {
            System.out.println("  删除后无举报。");
        }
        System.out.println();

        System.out.println("=== 9. 从评论跳转到商品 ===");
        // 先查询所有评论
        List<Comment> allComments = commentService.findAll();
        if (allComments != null && !allComments.isEmpty()) {
            // 获取第一条评论
            Comment firstComment = allComments.get(0);
            System.out.println("找到评论：" + firstComment);
            
            // 通过评论获取商品信息
            if (firstComment.getGoods() != null) {
                Goods commentGoods = goodsService.findById(firstComment.getGoods().getId());
                if (commentGoods != null) {
                    System.out.println("跳转到商品：" + commentGoods.getName());
                    System.out.println("商品详情：");
                    System.out.println("  ID: " + commentGoods.getId());
                    System.out.println("  名称: " + commentGoods.getName());
                    System.out.println("  描述: " + commentGoods.getDescription());
                } else {
                    System.out.println("未找到对应的商品信息");
                }
            } else {
                System.out.println("评论中没有关联商品信息");
            }
        } else {
            System.out.println("当前没有评论记录");
        }

        // 关闭上下文
        context.close();
    }
}